unit uERP_InstanceClass;

interface

uses uERP_Usuario, uQuery, uERP_Contador;

type
  TInstanceClass = class

    {$REGION '<< Private >>'}

      private

        {$REGION '<< Atributes >>'}

          strict private class var _Instance: TInstanceClass;

        {$ENDREGION}

        {$REGION '<< Methods >>'}

          private function GetFieldValue(const a_UniQuery: TMyQuery; const a_FieldName: String): Variant;

        {$ENDREGION}

    {$ENDREGION}

    {$REGION '<< Public >>'}

      public

        {$REGION '<< Methods >>'}

          class function GetInstance(): TInstanceClass;
          class procedure FreeInstance();

          function NewUsuario(const a_UniQuery: TMyQuery): TUsuario;
          function NewContador(const a_UniQuery: TMyQuery): TContador;

        {$ENDREGION}

    {$ENDREGION}

  end;

implementation

{$REGION '<< TInstanceClass >>'}

uses uERP_Constantes, SysUtils;

class procedure TInstanceClass.FreeInstance;
begin
  FreeAndNil(_Instance);
end;

function TInstanceClass.GetFieldValue(const a_UniQuery: TMyQuery; const a_FieldName: String): Variant;
begin
  if Assigned(a_UniQuery.FindField(a_FieldName)) and not a_UniQuery.FindField(a_FieldName).IsNull
    then Result := a_UniQuery.FieldByName(a_FieldName).AsVariant;
end;

class function TInstanceClass.GetInstance: TInstanceClass;
begin
  if not Assigned(_Instance)
    then _Instance := inherited Create as Self;

  Result := _Instance;
end;

function TInstanceClass.NewContador(const a_UniQuery: TMyQuery): TContador;
begin
  Result := TContador.Create();

  Result.ID := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_ID);
  Result.Codigo := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_Codigo);
  Result.Nome := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_Nome);
  Result.Endereco := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_Endereco);
  Result.Numero := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_Numero);
  Result.Complemento := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_Complemento);
  Result.Bairro := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_Bairro);
  Result.Email := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_Email);
  Result.CEP := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_CEP);
  Result.DDDTelefone := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_DDDTelefone);
  Result.Telefone := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_Telefone);
  Result.DDDFax := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_DDDFax);
  Result.Fax := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_Fax);
  Result.DataDeRegistro := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_DataDeRegistro);
  Result.CRC_UF := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_CRC_UF);
  Result.CRC_Numero := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_CRC_Numero);
  Result.Documento := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_Documento);
  Result.RG := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Contador_RG);
  Result.ID_Municipio := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_WAS_Contador_Municipio);
end;

function TInstanceClass.NewUsuario(const a_UniQuery: TMyQuery): TUsuario;
begin
  Result := TUsuario.Create();

  Result.ID := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Usuario_ID);
  Result.Login := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Usuario_Login);
  Result.Senha := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Usuario_Senha);
  Result.Nome := Self.GetFieldValue(a_UniQuery, uERP_Constantes.c_Field_Usuario_Nome);
end;

{$ENDREGION}

initialization

finalization
  TInstanceClass.GetInstance().FreeInstance();

end.
